package com.bbdjt.weixin.web.action.city;


import org.apache.log4j.Logger;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;

import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.City;
import com.bbdjt.weixin.model.CityQueryModel;
import com.bbdjt.weixin.service.CityService;

/**
 * @项目名称: bbdjt
 * @类名称: CityAction
 * @类描述: City表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:42
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class CityAction extends BaseAction {
	
	@Autowired
	private CityService cityService;

	private String page_list = "jsp/city/cityindex";

	private String redirect_list = "redirect:r_city";
	
	
	// 分页查询
	@RequestMapping(value = "r_city")
	public ModelAndView getListPage(CityQueryModel city) {
		ModelAndView result = new ModelAndView(page_list);
		city.setOrderBy("id asc");
		List<City> cityList = cityService.selectByCity(city);
		result.addObject("pageInfo", new PageInfo<City>(cityList));
		result.addObject("city", city);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_cityXml")
	public ModelAndView getListPageXml(CityQueryModel city) {
		ModelAndView result = new ModelAndView(page_list);
		city.setOrderBy("id asc");
		PageInfo<CityQueryModel> pageInfo = cityService.selectByCityQueryModel(city);
		List<CityQueryModel> cityList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<CityQueryModel>(cityList));
		result.addObject("city", city);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_cityJson", produces = "application/json")
	public ModelAndView pageInfo(CityQueryModel city) {
		ModelAndView result = new ModelAndView();
		PageInfo<CityQueryModel> pageInfo = cityService.selectByCityQueryModel(city);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_cityLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(CityQueryModel city) {
		city.setOrderBy(city.getSortname(),city.getSortorder());
		PageInfo<CityQueryModel> pageInfo = cityService.selectByCityQueryModel(city);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_city")
	public ModelAndView add(City city) {
		ModelAndView result = new ModelAndView();
		if (city.getId() != null) {
			city = cityService.selectByKey(city.getId());
		}
		result.addObject("city", city);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_city")
	public ModelAndView save(City city) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (city.getId() != null) {
			cityService.updateNotNull(city);
		} else {
			// 会保存null 值
			cityService.insertSelective(city);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_city")
	public String delete(Integer id) {
		cityService.delete(id);
		return redirect_list;
	}
	
}